package com.qf.admin.servlet;

import com.qf.admin.domain.Pay;
import com.qf.admin.service.PayService;
import com.qf.admin.service.impl.PayServiceImpl;
import com.qf.base.BaseServlet;
import com.qf.commons.Data;
import com.qf.commons.TableData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/payPage")
public class GetPayServlet extends BaseServlet {

    PayService payService = new PayServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String methodName = req.getParameter("method");

        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public void getAllPay(HttpServletRequest req, HttpServletResponse resp){
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String status = req.getParameter("status");
        String begin = req.getParameter("begin");
        String end = req.getParameter("end");
        // 每页显示的数据,  limit 后的第一个参数 limit 0, 10
        String limit = req.getParameter("limit");
        // 偏移量 limit 后的第一个参数 limit 0, 10
        String offset = req.getParameter("offset");

        if(null == status) {
            status = "-1";
        }

        Date beginDate = null;
        Date endDate = null;

        if (null != begin && !"".equals(begin.trim())) {
            beginDate = dateFormat(begin, "yyyy-MM-dd HH:mm:ss");
        }

        if (null != end && !"".equals(end.trim())) {
            endDate = dateFormat(end, "yyyy-MM-dd HH:mm:ss");
        }

        TableData<Pay> allPay = payService.getAllPay(Integer.parseInt(limit), Integer.parseInt(offset));

        try {
            sendJson(resp,allPay);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Date dateFormat(String dateStr, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    public void deletePay(HttpServletRequest req, HttpServletResponse resp){

        Map<String, Object> map = new HashMap<>();

        String userId = req.getParameter("userId");

        try{
            payService.deletePay(Integer.parseInt(userId));
            map.put("code", 1);
            map.put("msg", "success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", -1);
            map.put("msg", "fail");
        }

        try {
            sendJson(resp,map);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

     public void updatePay(HttpServletRequest req, HttpServletResponse resp){
         Map<String, Object> map = new HashMap<>();
         String userId = req.getParameter("userId");

         try{
             payService.updatePay(Integer.parseInt(userId));
             map.put("code", 1);
             map.put("msg", "success");
         }catch (Exception e){
             e.printStackTrace();
             map.put("code", -1);
             map.put("msg", "fail");
         }

         try {
             sendJson(resp,map);
         } catch (ServletException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }

     }
}
